package com.cy.pj.sys.service.realm;

import com.cy.pj.sys.dao.SysMenuDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.dao.SysUserDao;
import com.cy.pj.sys.dao.SysUserRoleDao;
import com.cy.pj.sys.entity.SysUser;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class ShiroUserRealm extends AuthorizingRealm {
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;
    @Autowired
    private SysMenuDao sysMenuDao;

    /**负责授权信息的获取和封装*/
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //获取登录用户信息
        SysUser user = (SysUser)principals.getPrimaryPrincipal();
        Integer userId = user.getId();
        //基于登录用户id获取用户角色id并进行判定
        List<Integer> roleIds = sysUserRoleDao.findRoleIdsByUserId(userId);
        if(roleIds==null||roleIds.size()==0)
            throw new AuthorizationException();

        Integer[] array={};
        List<Integer> menuIds=sysRoleMenuDao.findMenuIdsByRoleIds(roleIds.toArray(array));
        if(menuIds==null||menuIds.size()==0)
            throw new AuthorizationException();

        List<String> permissions = sysMenuDao.findPermissions(menuIds.toArray(array));

        Set<String> set = new HashSet<>();
        for(String per:permissions){
            if(!StringUtils.isEmpty(per)){
                set.add(per);
            }
        }
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(set);
        return info;
    }

    /**负责认证信息的获取和封装*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken upToken = (UsernamePasswordToken)token;
        String username = upToken.getUsername();
        //基于用户名获取用户对象
        SysUser user = sysUserDao.findUserByUserName(username);
        //判定用户对象是否存在
        if(user==null)
            throw new UnknownAccountException();
        //判定用户是否有效
        if(user.getValid()==0)
            throw new LockedAccountException();
        //封装用户认证信息
        ByteSource credentialsSalt =  ByteSource.Util.bytes(user.getSalt());
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(
                user,
                user.getPassword(),
                credentialsSalt,
                getName());

        return info;
    }

    //负责获取加密凭证匹配器
    @Override
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher){
        HashedCredentialsMatcher hMatcher = new HashedCredentialsMatcher();
        hMatcher.setHashAlgorithmName("MD5");
        hMatcher.setHashIterations(1);
        super.setCredentialsMatcher(hMatcher);
    }
}
